package org.fkjava.oa.identity.action;

import java.util.List;

import javax.annotation.Resource;

import org.fkjava.common.util.pager.PageModel;
import org.fkjava.oa.identity.domain.Module;
import org.fkjava.oa.identity.domain.Role;
import org.fkjava.oa.identity.domain.User;
import org.fkjava.oa.identity.service.IdentityService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/identity/popedom")
public class PopedomAction {
	/** 1.定义业务层对象 */
	@Resource // by type
	private IdentityService identityService;
	
	@RequestMapping(value="/mgrPopedom.jspx")
	public String mgrPopedom(Role role,Model model){
		try {
			role = identityService.getRoleById(role.getId());
			model.addAttribute("role", role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "identity/role/bindPopedom/mgrPopedom";
	}
	
	// getOperasByParent.jspx  逗逼式 -> 优化式 -> 文艺式
	@RequestMapping(value="/getOperasByParent.jspx")
	public String getOperasByParent(
			@RequestParam("parentCode")String parentCode,
			@RequestParam("moduleName")String moduleName,
			Role role,Model model){
		try {
			// 普通(UTF-8:) [000] [000] ---> ISO-8859-1编码  [00] [00] [00] ---> "ISO-8859-1"
			//  [000000] --> UTF-8 ---> [000] [000] 
			role.setName(new String(role.getName().getBytes("ISO-8859-1"),"UTF-8"));
			moduleName = new String(moduleName.getBytes("ISO-8859-1"),"UTF-8");
			List<Module> sonModules = identityService.getModulesByParent(parentCode,null);
			/** 还应该查询出当前role在当前模块下parentCode拥有哪些操作权限 
			 *  去控制界面的权限显示 
			 *  --- 查询出当前角色role在parentCode模块下拥有的操作编号即可。
			 *  [000100010001,000100010002]
			 * */
			List<String> roleModuleOperasCodes = identityService.getRoleModuleOperasCodes(role,parentCode);
			model.addAttribute("modules", sonModules);
			model.addAttribute("moduleName", moduleName);
			model.addAttribute("parentCode", parentCode);
			model.addAttribute("roleModuleOperasCodes", roleModuleOperasCodes+"");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "identity/role/bindPopedom/operas";
	}
	
	@RequestMapping(value="/bindPopedom.jspx")
	public String bindPopedom(@RequestParam("codes")String codes,
			@RequestParam("parentCode")String parentCode,Role role,Model model){
		try {
			identityService.bindPopedom(codes,role,parentCode);
			model.addAttribute("tip", "绑定成功");
		} catch (Exception e) {
			model.addAttribute("tip", "绑定失败");
			e.printStackTrace();
		}
		return "forward:/identity/popedom/getOperasByParent.jspx";
	}
	
}





